# coding: UTF-8
# Filename : chain_commentlist_api.py
# Company : imedia
# Author : rose # -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl, err_tpl
import chain_commentlist_data_model
import time
'''
http://api.imedia.com/app/part/list
'''
		
#details list
def chain_commentlist_list(request):
    #record log((frist action))
    httputils.footprint(request)

    if validate(request):
        return validate(request)

    #param
    cus_id = httputils.post(request, 'cus_id')#is customer id,more
    target = httputils.post(request, 'target')#is customer id,more
    target_id = httputils.post(request, 'target_id')#is customer id,more
    size = httputils.post(request, 'size')
    pagenum = httputils.post(request, 'pagenum')
    if not utils.is_num(size): size = 5
    if not utils.is_num(pagenum): pagenum = 1
    if not utils.is_num(target): target = 1
    #if not utils.is_num(target_id): target_id = 1
    if not utils.is_num(cus_id): cus_id = 1 
    #Verification
    output = chain_commentlist_data_model.imedia_start 
    output += get_body_str(cus_id, target, target_id, size, pagenum)
    output += chain_commentlist_data_model.imedia_end
    return response(output) 
#获取中间数据体
def get_body_str(cus_id, target, target_id, size, pagenum):
    '''
    展示 在后台已审核的数据
    '''
    #如果target_id  如果为空，则返回所有的针对该商品、分店或者企业的评论
    start_row = utils.get_star_row(size, pagenum)
    target_id_sql = ''
    if  utils.is_num(target_id): 
        target_id_sql = 'and target_id= %s' 
        param = [int(cus_id), int(target), int(target_id), start_row, int(size)]
    else:
        param = [int(cus_id), int(target),  start_row, int(size)]

    #sql
    sql = '''
    select a.`id`, user_id, score, created_at, content, target_id
    from chain_comment as a 
    where cus_id=%s and reply_id = 0  and target = %s  
    ''' + target_id_sql +'''
     and state = 1 order by id desc
    limit %s, %s
    '''
    rsts = mysql.doSearch(sql, param)
    body_str = ''
    i = 0
    for obj in rsts:
        i = i + 1
        body_template = chain_commentlist_data_model.imedia_body
        body_template = body_template.replace('{id}', str(obj[0]))
        uname = get_username(obj[1])
        body_template = body_template.replace('{user}', str(uname))
        body_template = body_template.replace('{score}', str(obj[2]))
        ISOTIMEFORMAT='%Y-%m-%d %X'
        start_time = time.strftime(ISOTIMEFORMAT,time.localtime(obj[3]))
        body_template = body_template.replace('{datetime}', start_time)
        body_template = body_template.replace('{content}', str(obj[4]))
        body_template += get_replay_str(int(obj[0])) 
        body_str += body_template
        body_str += chain_commentlist_data_model.imedia_body_end
    #body_str = body_str[0:-10] 
    return body_str

def get_username(user_id):
    '''
    取得用户表中手机号码
    '''
    sql = '''
    select username from user where id = %s
    '''
    param = [int(user_id)]
    rsts = mysql.get(sql, param)
    return rsts[0]




def get_replay_str(reply_id): 
    '''
    获取评论下的回复信息
    这里过滤app 端回复  God called you to do so may
    reply_id <> 0 取出所有回复记录
    source 等于 2 表示 后台回复
    source 等于 1 表示 app回复

    '''
    #sql
    sql = '''
    select created_at, content, source ,user_id
    from chain_comment 
    where reply_id=%s and reply_id <> 0 order by id desc 
    '''
    #获取起始行
    #start_row = utils.get_star_row(size, pagenum)
    param = [int(reply_id)]
    rsts = mysql.doSearch(sql, param)
    body_str = ''
    for obj in rsts:
        if obj[2] == 2:
            body_template = chain_commentlist_data_model.imedia_body_reply
        else:
            body_template = chain_commentlist_data_model.imedia_app_body_reply
            body_template = body_template.replace('{tel}', str(get_username(obj[3])))
            
        body_template = body_template.lstrip()
        ISOTIMEFORMAT='%Y-%m-%d %X'
        start_time = time.strftime(ISOTIMEFORMAT,time.localtime(obj[0]))
        body_template = body_template.replace('{datetime}', start_time)
        body_template = body_template.replace('{content}', str(obj[1]))
        body_str += body_template 
    return body_str   
     


